package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.AttendanceConfig;
import com.itheima.pojo.Result;
import com.itheima.pojo.Salary;
import com.itheima.service.AttendanceConfigService;
import com.itheima.service.SalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/salary")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;
    @PostMapping("/add")
    public Result add(@RequestBody Salary salary) {
        salaryService.save(salary);
        return Result.success("成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        salaryService.removeById(id);
        return Result.success("成功");
    }

    @PutMapping("/put/{id}")
    public Result update(@PathVariable Long id, @RequestBody Salary salary) {
        // 校验 ID 是否匹配
        if (id == null) {
            return Result.error("请求的ID与数据中ID不一致");
        }

        // 更新操作
        boolean isUpdated = salaryService.updateById(salary);

        if (isUpdated) {
            return Result.success("成功");
        } else {
            return Result.error("更新失败");
        }
    }

    // 获取薪资信息
    @GetMapping("/detail/{id}")
    public Result getSalaryById(@PathVariable Long id) {
        // 根据 ID 查询薪资信息
        Salary salary = salaryService.getById(id);

        // 如果没有找到对应的薪资信息，返回错误信息
        if (salary == null) {
            return Result.error("未找到该薪资信息");
        }

        // 返回成功的响应，包含薪资信息
        return Result.success(salary);
    }
    //工资管理列表

    @GetMapping("/list")
    public Result<Page> page(Integer page, Integer pagesize, String keyword) {
        Page page1 = new Page(page, pagesize);
        return salaryService.selectPage(page1, keyword);

    }
    //我的工资列表
    @GetMapping("/myList")
    public Result<Page> page(Integer page, Integer pagesize, Integer employeeId,String month) {
        Page page1 = new Page(page, pagesize);
        return salaryService.selectPage1(page1, employeeId, month);

    }
}
